package com.neuedu.dao.Imp;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;
import com.neuedu.dao.EmployeeDao;
import com.neuedu.pojo.Business;
import com.neuedu.pojo.Employee;
import com.neuedu.pojo.Food;
import com.neuedu.utils.JDBCUtil;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class EmployeeDaoImp implements EmployeeDao {
    private DataSource dataSource = JDBCUtil.getDruidDataSource();
    // 查询数据
    @Override
    public List<Employee> getEmpAll() throws Exception {

        List<Employee> employees = Db.use(dataSource).query("select * from employee",Employee.class);
        return employees;
    }

    // 添加数据
    @Override
    public Integer addEmp(String account,String password,String nickname,String telephone,String role,int isDel) throws Exception {

        Integer x = Db.use(dataSource).insert(
          Entity.create("employee")
                  .set("id",null)
                  .set("account",account)
                  .set("password",password)
                  .set("nickname",nickname)
                  .set("telephone",telephone)
                  .set("role",role)
                  .set("is_del",isDel)
        );
        return x;
    }

    // 删除员工信息

    @Override
    public Integer delEmp(String id) throws Exception{

        Integer x = Db.use(dataSource).del(
                Entity.create("employee")
                        .set("id",id)
        );
        return x;
    }

    // 修改员工信息

    @Override
    public Integer update(String id, String account, String password, String nickname, String telephone, String role, String isDel) throws Exception{

        Integer x = Db.use(dataSource).update(
                Entity.create()
                        .set("account",account)
                        .set("password",password)
                        .set("nickname",nickname)
                        .set("telephone",telephone)
                        .set("role",role)
                        .set("is_del",isDel),
                Entity.create("employee")
                        .set("id",id)
        );
        return x;
    }

    @Override
    public Employee selectPar(String account, String password) throws Exception {
        return Db.use(dataSource).query("select * from employee where account = ? and password = ?", BeanHandler.create(Employee.class),account,password);
    }

    @Override
    public List<Employee> getEmp(String currentPage, String pageSize, String account, String nickname, String telephone) throws Exception {
        String sql = "select * from employee ";

        // 定义一个map集合，将搜索条件对应的列名作为map的key，搜索条件的值作为map的value
        Map<String,String> map = new HashMap<>();
        // 判断传递的参数
        if(StrUtil.isNotBlank(account)){
            map.put("account",account);
        }
        if(StrUtil.isNotBlank(nickname)){
            map.put("nickname",nickname);
        }
        if(StrUtil.isNotBlank(telephone)){
            map.put("telephone",telephone);
        }
        // 这里可以直接判断map是否为空
        if(!map.isEmpty()){
            sql += " where ";
            // 判断成立，说明map中有搜索的条件数据，这时只要遍历map，去给sql拼接条件即可
            Set<Map.Entry<String,String>> entries = map.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                sql += entry.getKey() + " like" + " '%" + entry.getValue() + "%' or ";
            }
            // sql的最后多or关键字 ，需要单独处理"select * from business where account like %?% or telephone like %?% or contact like %?% or "
            sql = sql.substring(0,sql.lastIndexOf("or"));
        }
        // "select * from business where account like %?% or telephone like %?% or contact like %?% limit 起始位置 , 条数"
        // 判断分页的参数、
        Integer page = 1;
        Integer size = 2;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if(StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        sql += " limit " + (page - 1) * size + " , " + size;
        return Db.use(dataSource).query(sql, Employee.class);
    }

    @Override
    public Long countEmp() throws Exception {
        return Db.use(dataSource).count(Entity.create("employee"));
    }
}
